<template>
  <div class="body">
    <div class="box">
      <el-form :model="user" :rules="rules" ref="loginRef">
        <div style="font-size: 20px;margin: 15px;font-weight: bold;text-align: center;">
          登入
        </div>
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            placeholder="请输入密码"
            v-model="user.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login">
            登入
          </el-button>
        </el-form-item>
        <div style="display: flex; height: 30px">
          <div style="flex: 1">
            <span style="color: #0f9876; cursor: pointer" @click="$router.push('/register')">
              注册
            </span>
          </div>
          <div style="flex: 1; text-align: right">
            <span style="color: #0f9876; cursor: pointer" @click="forgetPassword">
              忘记密码
            </span>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  components: {
    Lock,
  },
  neme: "Login",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          // { min: 8, max: 8, message: "请输入正确用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          // { min: 6, max: 15, message: "请输入正确密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    forgetPassword() {
      window.alert("请联系管理员QQ1952287329");
    },
    login() {
      this.$refs["loginRef"].validate((valid) => {
        if (valid) {
          // 验证通过
          request.post("/login", this.user).then((res) => {
            if (res.code === "200") {
              this.$store.dispatch("user/getLoginUser", res.data);
              this.$router.push("/");
            } else {
              window.alert(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style>
.body {
  height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(65, 171, 141, 0.289);
}
.box {
  display: flex;
  background-color: #F6F6F6;
  width: 300px;
  border-radius: 5px;
  overflow: hidden;
  justify-content: center;
  box-shadow: 0 4px 8px rgba(0.9, 0.9, 0.9, 0.2); /* 添加阴影 */
}
</style>
